package com.example.minefieldkeepercloud.activity.dailypower;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.bigkoo.pickerview.TimePickerView;
import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.adapter.DailyPowerAdapter;
import com.example.minefieldkeepercloud.bean.DailyPowerBean;
import com.example.minefieldkeepercloud.bean.FactoryBean;
import com.example.minefieldkeepercloud.interfac.ApiHttpCallback;
import com.example.minefieldkeepercloud.network.MyGson;
import com.example.minefieldkeepercloud.network.NetHelperNew;
import com.example.minefieldkeepercloud.util.NetWorkUtil;
import com.example.minefieldkeepercloud.util.SPUtilHelper;
import com.example.minefieldkeepercloud.util.ToastUtil;
import com.example.minefieldkeepercloud.view.pop.FactorySelectPop;
import com.example.minefieldkeepercloud.view.pop.FactoryUserPop;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 矿机日算力统计
 */
public class DailyPowerAc extends AppCompatActivity {
    private FactorySelectPop factorySelectPop;
    private FactoryUserPop factoryUserPop;
    @BindView(R.id.ly_top)
    LinearLayout ly_top;
    @BindView(R.id.recyleview)
    ListView recyleview;
    @BindView(R.id.tv_FactoryName)
    TextView tv_FactoryName;
    @BindView(R.id.tv_FullName)
    TextView tv_FullName;
    @BindView(R.id.tv_date)
    TextView tv_date;
    @BindView(R.id.edit)
    EditText edit;
    private DailyPowerAdapter dailyPowerAdapter;
    private List<DailyPowerBean.DataBean> listData;
    private TimePickerView pvTimeDay;//年月日时间选择器
    private String FactoryId="";
    private String FactoryUserId="-1";
    private SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
    private String NowData="";



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_dailypower);
        ButterKnife.bind(this);
        getMineField();
        init();

    }

    private void init() {
        listData = new ArrayList<>();
        dailyPowerAdapter = new DailyPowerAdapter(DailyPowerAc.this, listData);
        recyleview.setAdapter(dailyPowerAdapter);
        //时间选择器
        pvTimeDay = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置范围
        pvTimeDay.setRange(2017, 2030);

        //设置是否循环滚动
        pvTimeDay.setCyclic(false);
        pvTimeDay.setCancelable(true);

        //时间选择后回调
        pvTimeDay.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                NowData=ymd.format(date);
                tv_date.setText(NowData);
                if (!NetWorkUtil.check(DailyPowerAc.this)) {
                    return;
                }

                if(edit.getText().toString().isEmpty())
                {
                    ToastUtil.showToast(DailyPowerAc.this,"请输入保证率");
                    return;
                }
                getNetData();

            }


        });
        Date d=new Date();
//        NowData = ymd.format(new Date(d.getTime() - (long)24 * 60 * 60 * 1000));
        NowData = ymd.format(new Date(d.getTime()));
        tv_date.setText(NowData);

        recyleview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position==listData.size()-1)
                {
                    return;
                }
                if(listData.get(position).getSjzxgs().equals("-"))
                {
                    ToastUtil.showToast(DailyPowerAc.this, "没有矿场相关数据");
                    return;
                }
                DailyPowerDetailAc.start(DailyPowerAc.this,listData.get(position).getFactoryID()+"",listData.get(position).getUserID()+"",listData.get(position).getBzl(),NowData);
            }
        });
        /**
         * 设置保证率
         */
        if(SPUtilHelper.getBzl().isEmpty())
            edit.setText("95");
        else
            edit.setText(SPUtilHelper.getBzl());

        edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                SPUtilHelper.setBzl(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void getMineField() {
        if (!NetWorkUtil.check(this)) {
            return;
        }
        NetHelperNew.GetFactory(new ApiHttpCallback(DailyPowerAc.this, 1,true) {
            @Override
            public void onSuccess(String data) {
                FactoryBean factoryBean = MyGson.getInstance().fromJson(data, FactoryBean.class);
                if (factoryBean.getType() == 1) {
                    if (factoryBean.getData().size() > 0) {
                        CreatePop(factoryBean);
                    } else {
                        ToastUtil.showToast(DailyPowerAc.this, "没有可查询的矿场");
                    }

                } else {
                    ToastUtil.showToast(DailyPowerAc.this, factoryBean.getContent());
                }
            }
        });


    }

    @OnClick({R.id.ly_Minefield,R.id.ly_user,R.id.search,R.id.ll_date,R.id.title_left})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ly_Minefield:
                if (factorySelectPop != null)
                    factorySelectPop.showAsDropDown(ly_top, 0, 0);
                else
                    ToastUtil.showToast(DailyPowerAc.this, "没有可查询的矿场");
                break;
            case R.id.ly_user:
                if (factoryUserPop != null)
                    factoryUserPop.showAsDropDown(ly_top, 0, 0);
                else
                    ToastUtil.showToast(DailyPowerAc.this, "没有可查询的用户");
                break;
            case R.id.search:
                if (!NetWorkUtil.check(this)) {
                    return;
                }

                if(edit.getText().toString().isEmpty())
                {
                    ToastUtil.showToast(DailyPowerAc.this,"请输入保证率");
                    return;
                }
                getNetData();
                break;
            case R.id.ll_date:
                String nowdate = ymd.format(new Date(System.currentTimeMillis()));
                Log.d("===",nowdate);
                pvTimeDay.setTime(new Date(System.currentTimeMillis()));
                pvTimeDay.show();
                break;
            case R.id.title_left:
               finish();
                break;
        }
    }

    private void CreatePop(FactoryBean dataBean) {
        if(FactoryId.isEmpty())
        {
            FactoryId=dataBean.getData().get(0).getID()+ "";
        }
        if (factorySelectPop == null) {
            factorySelectPop = new FactorySelectPop(DailyPowerAc.this, dataBean, new FactorySelectPop.OnSelectListener() {
                @Override
                public void select(int id, String name) {
                    tv_FactoryName.setText(name);
                    FactoryId=id+"";
                    factoryUserPop=null;
                    CreatUserPop();
                }
            });
        }
        CreatUserPop();

        tv_FactoryName.setText(dataBean.getData().get(0).getFactoryName());

        getNetData();
    }

    private void CreatUserPop()
    {
        if(factoryUserPop==null)
        {
            factoryUserPop=new FactoryUserPop(DailyPowerAc.this, FactoryId, new FactoryUserPop.OnSelectListener() {
                @Override
                public void select(int id, String name) {
                    tv_FullName.setText(name);
                    FactoryUserId=id+"";
                }
            });

        }
    }

    private void getNetData()
    {

        NetHelperNew.GetCalPoweStatistics_Table(FactoryId, FactoryUserId, edit.getText().toString(), NowData, new ApiHttpCallback(DailyPowerAc.this, 1, true) {
            @Override
            public void onSuccess(String data) {
                DailyPowerBean dailyPowerBean = MyGson.getInstance().fromJson(data, DailyPowerBean.class);
                if (dailyPowerBean.getType() == 1) {
                    listData = dailyPowerBean.getData();
                    dailyPowerAdapter.setListData(listData);
                    dailyPowerAdapter.notifyDataSetChanged();
                } else {
                    ToastUtil.showToast(DailyPowerAc.this, dailyPowerBean.getContent());
                }
            }
        });

    }

}
